﻿using System;
using System.Linq;
using System.Windows.Forms;
using AppAcc.Models;
using AppAcc.Views.DanhMuc;

namespace AppAcc.Views.KTTSCD
{
    public partial class FormPhanBoKhauHao : DevComponents.DotNetBar.RibbonForm
    {
        private PhanMenKeToanEntities1 db = new PhanMenKeToanEntities1();
        public FormPhanBoKhauHao()
        {
            InitializeComponent();
        }
        #region Event
        private void cbbKyTinhKhauHao_SelectedValueChanged(object sender, EventArgs e)
        {
            txtNoiDung.Text = cbbKyTinhKhauHao.SelectedText;
        }
        private void bttTinhKhauHao_Click(object sender, EventArgs e)
        {
            TinhKhauHao();
        }

        private void bttThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void cbbNhanVien_SelectedValueChanged(object sender, EventArgs e)
        {
            KiemTra();
        }

        private void bttThemMoiNhanVien_Click(object sender, EventArgs e)
        {
            var f = new FormNhanVienThem();
            f.ShowDialog();
        }

        private void txtNoiDung_TextChanged(object sender, EventArgs e)
        {
            KiemTra();
        }
        private void FormPhanBoKhauHao_Load(object sender, EventArgs e)
        {
            LoadTaiSanTinhKhauHao();
            FillData();
        }
        #endregion Event
        #region Metho

        void LoadTaiSanTinhKhauHao()
        {
            dgvTaiSan.DataSource = db.TaiSans.Where(t => t.TrangThai == 1).ToList();
            KiemTra();
        }
        void KiemTra()
        {
            if (txtNoiDung.Text != "" && Convert.ToInt32(cbbNhanVien.SelectedValue) != 0 && dgvTaiSan.RowCount > 1 && dtiNgayChungTu.Value.ToString().Length > 0 && dtiNgayLapPhieu.Value.ToString().Length > 0)
            {
                bttTinhKhauHao.Enabled = true;
            }
            else
            {
                bttTinhKhauHao.Enabled = false;
            }
        }
        void TinhKhauHao()
        {
            var listTaiSanKhauHao = db.TaiSans.Where(t => t.TrangThai == 1).ToList();
            foreach (var taiSan in listTaiSanKhauHao)
            {
                var pt = new ChungTu();
                var mact = db.ChungTus.Count(t => t.TrangThai == 3) + 1;
                pt.Id = "CTKH" + mact.ToString();
                pt.NgayChungTu = dtiNgayChungTu.Value;
                pt.NgayLapPhieu = dtiNgayLapPhieu.Value;
                pt.NhanVienId = Convert.ToInt32(cbbNhanVien.SelectedValue);
                pt.NoiDung = txtNoiDung.Text;
                pt.TrangThai = 3;
                decimal soTien = 0;
                pt.TongTien = 0;
                for (int i = 0; i < 2; i++)
                {
                    var ct = new ChungTuChiTiet();
                    ct.ChungTuId = pt.Id;
                    if (i == 0)
                    {
                        ct.TaiKhoanNo = "214";
                        ct.TaiSanId = taiSan.Id;
                        var ttdv = db.ThongTinDonViKeToans.Find(1);
                        switch (ttdv.ChuKyKhauHao)
                        {
                            case 1:
                                soTien = (decimal) ((taiSan.GiaTriKhauHao / taiSan.ThoiGianKhauHao) / 12);
                                break;
                            case 2:
                                soTien = (decimal) ((taiSan.GiaTriKhauHao / taiSan.ThoiGianKhauHao) / 4);
                                break;
                            default:
                                {
                                    soTien = (decimal) (taiSan.GiaTriKhauHao / taiSan.ThoiGianKhauHao);
                                }
                                break;
                        }
                        ct.SoTien = soTien;
                        var pb = db.ChungTuChiTiets.Where(t => t.TaiSanId == taiSan.Id && t.PhongBanId != 0);
                        var idpb = 0;
                        if (pb.Count() == 1)
                        {
                            foreach (var j in pb)
                            {
                                idpb = (int)j.PhongBanId;
                            }
                        }
                        ct.PhongBanId = idpb;
                        ct.DienGiai = "Khấu hao tài sản.";
                    }
                    if (i == 1)
                    {
                        ct.TaiKhoanCo = "627";
                        ct.SoTien = soTien;
                        ct.DienGiai = "Khấu hao tài sản.";
                    }
                    db.ChungTuChiTiets.Add(ct);
                }
                pt.TongTien = soTien;
                db.ChungTus.Add(pt);
                try
                {
                    db.SaveChanges();
                    MessageBox.Show("Tính khấu hao thành công!");
                }
                catch (Exception e)
                {
                    MessageBox.Show("Quá trình xử lý xảy ra lỗi.!");
                    MessageBox.Show(e.Message);
                }
            }
        }

        void FillData()
        {
            dtiNgayChungTu.Value = DateTime.Now;
            dtiNgayLapPhieu.Value = DateTime.Now;
            cbbNhanVien.DataSource = db.NhanViens.ToList();
            cbbNhanVien.DisplayMember = "TenNhanVien";
            cbbNhanVien.ValueMember = "Id";
            var ttdv = db.ThongTinDonViKeToans.Find(1);
            switch (ttdv.ChuKyKhauHao)
            {
                case 1:
                    cbbKyTinhKhauHao.Items.Add(new Item("Chọn kỳ tính khấu hao", 0));
                    cbbKyTinhKhauHao.Items.Add(new Item("Khấu hao tháng 1", 1));
                    cbbKyTinhKhauHao.Items.Add(new Item("Khấu hao tháng 2", 2));
                    cbbKyTinhKhauHao.Items.Add(new Item("Khấu hao tháng 3", 3));
                    cbbKyTinhKhauHao.Items.Add(new Item("Khấu hao tháng 4", 4));
                    cbbKyTinhKhauHao.Items.Add(new Item("Khấu hao tháng 5", 5));
                    cbbKyTinhKhauHao.Items.Add(new Item("Khấu hao tháng 6", 6));
                    cbbKyTinhKhauHao.Items.Add(new Item("Khấu hao tháng 7", 7));
                    cbbKyTinhKhauHao.Items.Add(new Item("Khấu hao tháng 8", 8));
                    cbbKyTinhKhauHao.Items.Add(new Item("Khấu hao tháng 9", 9));
                    cbbKyTinhKhauHao.Items.Add(new Item("Khấu hao tháng 10", 10));
                    cbbKyTinhKhauHao.Items.Add(new Item("Khấu hao tháng 11", 11));
                    cbbKyTinhKhauHao.Items.Add(new Item("Khấu hao tháng 12", 12));
                    break;
                case 2:
                    cbbKyTinhKhauHao.Items.Add(new Item("Chọn kỳ tính khấu hao", 0));
                    cbbKyTinhKhauHao.Items.Add(new Item("Khấu hao quý 1", 1));
                    cbbKyTinhKhauHao.Items.Add(new Item("Khấu hao quý 2", 2));
                    cbbKyTinhKhauHao.Items.Add(new Item("Khấu hao quý 3", 3));
                    cbbKyTinhKhauHao.Items.Add(new Item("Khấu hao quý 4", 4));
                    break;
                default:
                    {
                        var nam = DateTime.Now.Year.ToString();
                        cbbKyTinhKhauHao.Items.Add(new Item("Chọn kỳ tính khấu hao", 0));
                        cbbKyTinhKhauHao.Items.Add(new Item("Khấu hao năm " + nam, 1));
                    }
                    break;
            }
        }
        private class Item
        {
            private string Name;
            private int Value;
            public Item(string name, int value)
            {
                Name = name; Value = value;
            }
            public override string ToString()
            {
                // Generates the text shown in the combo box
                return Name;
            }
        }
        #endregion Metho


    }
}